<script setup>
	import {
		ref,
		onMounted
	} from 'vue'; // 注意：这里使用的是 uni-ui 的 showToast，实际路径可能因库而异
	import {
		useUserStore
	} from '@/stores'
	import {
		apiWxLogin,
		apiCheckBind
	} from "@/api/user.js";
	const userStore = useUserStore()
	// 使用 ref 创建响应式数据
	const account = ref(''); // 账号
	const pwd = ref(''); // 密码

	// 登录函数
	const login = async () => {
		uni.login({
				provider: 'weixin',
				success: async res => {
					if (res.errMsg === "login:ok") {
						console.log(res)
						const response = await apiWxLogin({
							code: res.code
						})
						console.log("response=", response)


						if (response.code === 1) {
							userStore.setToken(response.data.token)
							const check = await apiCheckBind()

							if (check.data.isBind === 0) {
								//带确认的弹窗
								uni.showModal({
									title: '前往注册',
									content: '您是第一次登录，请前去注册账户',
									showCancel: true,
									cancelText: '取消',
									confirmText: '确定',
									mask: true,
									success: (res) => {
										if (res.confirm) {
											uni.navigateTo({
												url: '/pages/register/register',
											})
										}
									}
								})

							}
							userStore.getUser()
							uni.showToast({
								title: '登录成功！',
								icon: 'success'
							});
							uni.switchTab({
								url: '/pages/newIndex/newIndex'
							});
						} else {
							uni.showToast({
								title: response.msg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '登录失败！',
							icon: 'error'
						});
					}
				}
			}


		)
	}
	// 注册函数
	const reg = () => {

		uni.login({
				provider: 'weixin',
				success: async res => {
					if (res.errMsg === "login:ok") {
						const response = await apiWxLogin({
							code: res.code
						})
						userStore.setToken(response.data.token)
						if (response.code === 1) {
							const check = await apiCheckBind()
							if (check.data.isBind === 0) {
								uni.navigateTo({
									url: '/pages/register/register',
								})
							}
							uni.showToast({
								title: '您已经注册过账号！',
								icon: 'none'
							});
						}
					} else {
						uni.showToast({
							title: '注册失败！',
							icon: 'error'
						});
					}
				}
			}


		)
	};
	onMounted(() => {
		const userInfo = uni.getStorageSync('userInfo')
		if (userInfo) {
			const userData = JSON.parse(userInfo)
			// 如果您想自动登录（根据您的应用安全策略）
			userStore.setToken(userData.token)

		}
	})


	// 微信登录函数
	// const wxLogin = async () => {

	// 	uni.showLoading({
	// 		title: "正在登录...",
	// 		mask: true
	// 	})
	// 	uni.login({
	// 			provider: 'weixin',
	// 			success: async res => {
	// 				if (res.errMsg === "login:ok") {
	// 					const response = await apiWxLogin({
	// 						code: res.code
	// 					})
	// 					userStore.setToken(response.data.token)
	// 					uni.hideLoading();
	// 					if (response.code === 1) {
	// 						const check = await apiCheckBind()
	// 						if (check.data.isBind === 0) {
	// 							uni.navigateTo({
	// 								url: '/pages/login/bindUser'
	// 							})
	// 						} else {
	// 							userStore.getUser()
	// 							uni.showToast({
	// 								title: '登录成功！',
	// 								icon: 'success'
	// 							});
	// 							uni.switchTab({
	// 								url: '/pages/index/index'
	// 							});
	// 						}
	// 					} else {
	// 						uni.showToast({
	// 							title: '登录失败！',
	// 							icon: 'error'
	// 						});
	// 					}
	// 				}
	// 			}
	// 		}

	// 	)
	// }
</script>
<template>
	<view style="height:100vh;background: #fff;">
		<view class="img-a">
			<view class="t-b">
				您好，
				<br />
				欢迎使用，生动科普星小程序
			</view>
		</view>
		<view class="login-view" style="">
			<view class="t-login">
				<form class="cl">
					<!-- <view class="t-a">
						<text class="txt">账号</text>
						<input type="number" name="account" placeholder="请输入您的账号" maxlength="10" v-model="account" />
					</view>
					<view class="t-a">
						<text class="txt">密码</text>
						<input type="password" name="code" maxlength="15" placeholder="请输入您的密码" v-model="pwd" />
					</view> -->
					<button @tap="login()" style="background-color: #67c23a;margin-top: 300rpx;">微 信 一 键 登 录</button>
					<view class="reg" @tap="reg()">注 册</view>
				</form>
				<view class="t-f"><text>—————— 微信账号快捷登录 ——————</text></view>
				<view class="t-e cl">
					<view class="t-g" @tap="login()">
						<image src="https://zhoukaiwen.com/img/loginImg/wx.png"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<style>
	.txt {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}

	.img-a {
		width: 100%;
		height: 450rpx;
		background-image: url(https://zhoukaiwen.com/img/loginImg/head.png);
		background-size: 100%;
	}

	.reg {
		font-size: 28rpx;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
		background: #f5f6fa;
		color: #000000;
		text-align: center;
		margin-top: 30rpx;
	}

	.login-view {
		width: 100%;
		position: relative;
		margin-top: -120rpx;
		background-color: #ffffff;
		border-radius: 8% 8% 0% 0;
	}

	.t-login {
		width: 600rpx;
		margin: 0 auto;
		font-size: 28rpx;
		padding-top: 80rpx;
	}

	.t-login button {
		font-size: 28rpx;
		background: #2796f2;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
	}

	.t-login input {
		height: 90rpx;
		line-height: 90rpx;
		margin-bottom: 50rpx;
		border-bottom: 1px solid #e9e9e9;
		font-size: 28rpx;
	}

	.t-login .t-a {
		position: relative;
	}

	.t-b {
		text-align: left;
		font-size: 42rpx;
		color: #ffffff;
		padding: 130rpx 0 0 70rpx;
		font-weight: bold;
		line-height: 70rpx;
	}

	.t-login .t-c {
		position: absolute;
		right: 22rpx;
		top: 22rpx;
		background: #5677fc;
		color: #fff;
		font-size: 24rpx;
		border-radius: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 25rpx;
	}

	.t-login .t-d {
		text-align: center;
		color: #999;
		margin: 80rpx 0;
	}

	.t-login .t-e {
		text-align: center;
		width: 250rpx;
		margin: 80rpx auto 0;
	}

	.t-login .t-g {
		float: left;
		width: 100%;
	}

	.t-login .t-e image {
		width: 50rpx;
		height: 50rpx;
	}

	.t-login .t-f {
		text-align: center;
		margin: 150rpx 0 0 0;
		color: #666;
	}

	.t-login .t-f text {
		margin-left: 20rpx;
		color: #aaaaaa;
		font-size: 27rpx;
	}

	.t-login .uni-input-placeholder {
		color: #aeaeae;
	}

	.cl {
		zoom: 1;
	}

	.cl:after {
		clear: both;
		display: block;
		visibility: hidden;
		height: 0;
		content: '\20';
	}
</style>